package com.zxl.yjb.activity.home.sharemessage;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.ImagePagerActivity;
import com.zxl.yjb.adapter.FullyGridLayoutManager;
import com.zxl.yjb.adapter.NewsImageAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.bean.ShareMessageDetailBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.im.utils.RongChatUtils;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShareMessageDetailActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_source)
    TextView tv_source;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_content_list)
    RecyclerView tv_content_list;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.iv_confirm)
    ImageView iv_confirm;
    @BindView(R.id.iv_confirm2)
    ImageView iv_confirm2;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.ll_phone)
    TextView ll_phone;
    @BindView(R.id.iv_content)
    ImageView iv_content;



    private DataManager dataManager;
    private String id = "";
    private NewsImageAdapter newsImageAdapter;
    private ArrayList<String> imageList = new ArrayList<>();
    private ShareMessageDetailBean shareMessageDetailBean;

    private LoginSucessBean loginSucessBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_msg_detail);
        ButterKnife.bind(this);
        loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(ShareMessageDetailActivity.this, Const.VIPINFO);
        id = getIntent().getStringExtra("id");
        initView();

    }

    @Override
    protected void initView() {

        tv_page_title.setText("信息详情");
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setSmoothScrollbarEnabled(true);
//        linearLayoutManager.setAutoMeasureEnabled(true);
//        tv_content_list.setLayoutManager(linearLayoutManager);
        FullyGridLayoutManager manager = new FullyGridLayoutManager(ShareMessageDetailActivity.this, 3, GridLayoutManager.VERTICAL, false);
        tv_content_list.setLayoutManager(manager);
        tv_content_list.setHasFixedSize(true);
        tv_content_list.setNestedScrollingEnabled(false);
        newsImageAdapter = new NewsImageAdapter(R.layout.item_new_image, imageList);
        tv_content_list.setAdapter(newsImageAdapter);
        newsImageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                String url = (String) adapter.getItem(position);
                Intent intent = new Intent(getApplication(), ImagePagerActivity.class);
                // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, imageList);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                startActivity(intent);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });





    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DisplayUtil.dip2px(this,30),DisplayUtil.dip2px(this,30));
        params.rightMargin = DisplayUtil.dip2px(this,3);
        params.gravity = Gravity.CENTER;
        iv_confirm.setLayoutParams(params);

        iv_confirm.setVisibility(View.VISIBLE);
        iv_confirm.setImageResource(R.mipmap.zy_gxxx_scxx);

        getMessageDetail();
    }

    private void setView(ShareMessageDetailBean shareMessageDetailBean) {
        tv_title.setText(shareMessageDetailBean.getTitle());
        tv_source.setText("来源：" + shareMessageDetailBean.getNickname());
        tv_time.setText(shareMessageDetailBean.getAddtime());
        tv_content.setText(shareMessageDetailBean.getContent());
        ll_phone.setText(shareMessageDetailBean.getPhone());

        if (!(loginSucessBean.getUid()+"").equals(shareMessageDetailBean.getUid()))
        {
            tv_confirm.setVisibility(View.VISIBLE);
            tv_confirm.setText("沟通");
            tv_confirm.setTextColor(getResources().getColor(R.color.black_color));
            iv_confirm2.setVisibility(View.VISIBLE);
            iv_confirm2.setImageResource(R.mipmap.zy_gt);
        }
        if (imageList.size() == 0) {
            return;
        }
        if (imageList.size() <= 2) {
            tv_content_list.setVisibility(View.GONE);
            iv_content.setVisibility(View.VISIBLE);
            GlideApp.with(getApplication()).load(imageList.get(0)).into(iv_content);
        }else {
            imageList.remove(imageList.size()-1);
            iv_content.setVisibility(View.GONE);
            tv_content_list.setVisibility(View.VISIBLE);
            newsImageAdapter.notifyDataSetChanged();
        }

    }


    private void getMessageDetail() {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        ObserverOnNextListener<ShareMessageDetailBean> listener = new ObserverOnNextListener<ShareMessageDetailBean>() {
            @Override
            public void onNext(ShareMessageDetailBean shareMessageDetailBean) {
                imageList.addAll(shareMessageDetailBean.getImgs());
                ShareMessageDetailActivity.this.shareMessageDetailBean = shareMessageDetailBean;
                setView(shareMessageDetailBean);
            }
        };
        dataManager.getMessageDetail(new ProgressObserver<ShareMessageDetailBean>(listener, this), map);
    }

    private void setCollection()
    {
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("topicid", id);
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                ToastUtils.show(ShareMessageDetailActivity.this,"收藏成功");
            }
        };
        dataManager.setCollection(new ProgressObserver<Object>(listener,this),map);
    }

    private void callKeFe(final String phone) {
        ImageView kfPhone = new ImageView(this);
        kfPhone.setImageResource(R.mipmap.zy_dh);
        DisplayUtil.diaplayDialogWithView(this, "拨打电话", phone, "拨打", "取消", kfPhone, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                startActivity(intent);
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.iv_confirm,R.id.tv_confirm,R.id.iv_confirm2,R.id.ll_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_confirm:
                setCollection();
                break;
            case R.id.tv_confirm:
                shareMessageDetailBean.getUid();
                if (shareMessageDetailBean!=null);
                    RongChatUtils.startPrivateChat(ShareMessageDetailActivity.this, shareMessageDetailBean.getUid(), shareMessageDetailBean.getNickname(), null);
                break;
            case R.id.iv_confirm2:
                if (shareMessageDetailBean!=null);
                    RongChatUtils.startPrivateChat(ShareMessageDetailActivity.this, shareMessageDetailBean.getUid(), shareMessageDetailBean.getNickname(), null);
                break;
            case R.id.ll_phone:
                if (shareMessageDetailBean!=null)
                    callKeFe(shareMessageDetailBean.getPhone());
                else
                    ToastUtils.show(ShareMessageDetailActivity.this,"数据加载中，请稍后...");
                break;
        }
    }

    /*@OnLongClick(R.id.tv_content)
    public void onViewLongClick(View view)
    {

    }*/
}
